<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Promise-应用</title>
</head>

<body>
  <script>
    // Promise-生成器
    function load() {
      return new Promise(function(reslove, reject) {
        reslove("success")
      })
    }

    /* run(function* () {
      var result = yield load()
      console.log(result)
    }) */

    // ES8写法
    (async function() {
      var result = await load()
      console.log(result)
    })()

    // 图像加载处理
    function preImg(src) {
      return new Promise((reslove, reject) => {
        let img = new Image()
            img.src = src
            img.onload = function() {
              reslove(this)
            }
            img.onerror = function() {
              reject(this)
            }
      })
    }

    preImg("./images/load.png").then(res => {
      console.log(res)
    }).catch(err => {
      console.log(err)
    })
    
  </script>
</body>

</html>